//
//  Block.cpp
//  AvoidTheBlocks
//
//  Created by hoangtx on 6/18/13.
//
//

#include "Block.h"
#include "StaticVariables.h"

bool Block::init(){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double blockSize = winSize.width/NUMBER_OF_BLOCKS_ON_A_ROW;
    sprite = CCSprite::create("block.png");
    sprite->setScale(blockSize / sprite->getContentSize().width);
    sprite->setAnchorPoint(ccp(0,0));
    this->setAnchorPoint(ccp(0,0));
    this->addChild(sprite);
    
    return true;
}

void Block::setBlockToIndex(int index){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double blockSize = winSize.width/NUMBER_OF_BLOCKS_ON_A_ROW;
    this->setPosition(ccp(blockSize*index,winSize.height));

}

void Block::fallDown(cocos2d::CCArray *blockList, cocos2d::CCCallFuncO *callback){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    //CCArray* blockArrayOnColumn = (CCArray*)blockList->objectAtIndex(getColumnIndex());
    
    //CCPoint destinationPoint = ccp(this->getPositionX(),blockArrayOnColumn->count()*getBlockSize());
    //double distance = winSize.height - destinationPoint.y;
    double realDuration = (FALL_FROM_TOP_TO_BOTTOM_DURATION / winSize.height)*getBlockSize();
    CCDictionary* paramDict = CCDictionary::create();
    paramDict->setObject(blockList, "blocklist");
    paramDict->setObject(callback, "callback");
    CCMoveBy* moveAction = CCMoveBy::create(realDuration, ccp(0,-getBlockSize()));
    CCCallFuncO* miniCallBack = CCCallFuncO::create(this, callfuncO_selector(Block::checkIfItShouldContinueToFallDown), paramDict);
    CCSequence* sequenceAction = CCSequence::create(moveAction,miniCallBack,NULL);
    //CCRepeatForever* repeatAction = CCRepeatForever::create(sequenceAction);
    this->runAction(sequenceAction);
}

void Block::checkIfItShouldContinueToFallDown(cocos2d::CCObject *sender){
    CCDictionary* paramDict = (CCDictionary*)sender;
    CCCallFuncO* callback = (CCCallFuncO*)paramDict->objectForKey("callback");
    CCArray* blockList = (CCArray*)paramDict->objectForKey("blocklist");
    CCArray* blockArrayOnColumn = (CCArray*)blockList->objectAtIndex(this->getColumnIndex());
    if(getRowIndex() == blockArrayOnColumn->count()){

        this->stopAllActions();
        this->runAction(CCSequence::create(callback,NULL));
    }else{
        this->fallDown(blockList, callback);
    }
}

int Block::getColumnIndex(){

    return this->getPositionX()/getBlockSize();
}

int Block::getRowIndex(){

    return this->getPositionY()/getBlockSize();

}

double Block::getBlockSize(){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    return winSize.width/NUMBER_OF_BLOCKS_ON_A_ROW;

}

void Block::dropByDistance(double distance){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double realDuration = (FALL_FROM_TOP_TO_BOTTOM_DURATION / winSize.height)*distance;
    CCMoveBy* moveAction = CCMoveBy::create(realDuration, ccp(0,-distance));
    this->runAction(moveAction);
}

void Block::minimize(){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double realDuration = (FALL_FROM_TOP_TO_BOTTOM_DURATION / winSize.height)*getBlockSize();
    //CCFadeOut* fadeAction = CCFadeOut::create(realDuration);
    CCScaleTo* scaleAction = CCScaleTo::create(realDuration, 0);
    CCCallFuncO* callback = CCCallFuncO::create(this, callfuncO_selector(Block::removeFromParent), this);
    this->sprite->runAction(CCSequence::create(callback, NULL));
}

